package main

import (
	"fmt"
	pb "gitee.com/JaneCrysteal/grpcdemo/services"
	"net/rpc"
)

var (
	client *rpc.Client
	err    error
)

func init() {
	client, err = rpc.DialHTTP("tcp", "localhost:8090")

	if nil != err {
		panic(err.Error())
		return
	}

}

// syncGetGoodsInfo -- 获取同步商品信息
func syncGetGoodsInfo(client *rpc.Client, req pb.GetGoodsReq) (result *pb.GoodsDataInfo, err error) {
	err = client.Call("goodsService.GetGoodsInfo", req, &result)
	if nil != err || nil == result {
		fmt.Println(err)
		return
	}

	fmt.Println("商品编号:", result.Oid, "商品名称:", result.Name)

	return
}

// asyncGetGoodsInfo -- 异步获取商品信息
func asyncGetGoodsInfo(client *rpc.Client, req *pb.GetGoodsReq) (result *pb.GoodsDataInfo) {

	call := client.Go("goodsService.GetGoodsInfo", req, &result, nil)

	_ = <-call.Done

	//fmt.Println(resDone)

	fmt.Println("商品编号:", result.Oid, "商品名称:", result.Name)

	return
}

// main --
func main() {
	// 获取同步商品信息
	syncGetGoodsInfo(client, pb.GetGoodsReq{Oid: 10001})

	// 异步获取商品信息
	//asyncGetGoodsInfo(client, &pb.GetGoodsReq{Oid: 1001})

}
